<template>
  <view class="appBox">
    <view class="list-input">
      <form @submit.prevent="submit">
        <view class="item">
          <view class="acea-row row-middle">
            <input
              type="text"
              class="texts"
              placeholder="输入手机号"
              v-model="account"
              required
              disabled
            />
          </view>
        </view>
        <view class="item">
          <view class="acea-row row-middle">
            <input
              type="text"
              class="texts"
              placeholder="输入姓名"
              v-model="useRealRequest.name"
              required
            />
          </view>
        </view>
        <view class="item">
          <view class="acea-row row-middle">
            <input
              type="text"
              class="texts"
              placeholder="输入身份证号"
              v-model="useRealRequest.cardId"
              required
            />
          </view>
        </view>
		
      <!--  <view class="cards">
          <view
            class="card"
            v-model="useRealRequest.frontImg"
            @click="upLoadPic(1)"
			:style="getRealNames.frontImg ? 'pointer-events:none' : ''"
          >
            <image
              class="img"
              :src="getRealNames.frontImg"
              v-if="getRealNames.uid"
              mode="widthFix"
            ></image>
            <image
              class="img"
              :src="useRealRequest.frontImg"
              v-else-if="useRealRequest.frontImg"
              mode="widthFix"
            ></image>
            <image
              class="img"
              src="/static/images/zCard.png"
              v-else
              mode="widthFix"
            ></image>
            <text>身份证正面</text>
          </view>
          <view
            class="card"
            :style="getRealNames.oppositeImg ? 'pointer-events:none' : ''"
            v-model="useRealRequest.oppositeImg"
            @click="upLoadPic(2)"
          >
            <image
              class="img"
              :src="getRealNames.oppositeImg"
              v-if="getRealNames.uid"
              mode="widthFix"
            >
            </image>
            <image
              class="img"
              :src="useRealRequest.oppositeImg"
              v-else-if="useRealRequest.oppositeImg"
              mode="widthFix"
            ></image>
            <image
              class="img"
              src="/static/images/fCard.png"
              v-else
              mode="widthFix"
            ></image>
            <text>身份证反面</text>
          </view>
        </view>
      -->
	  
	  </form>
    </view>
    <button class="btn" @click="submit">提交</button>
  </view>
</template>

<script>
import store from "@/store/index.js";
import { realNameConfirm, getRealName,getUserInfo} from "@/api/user.js";
export default {
  name: "tradPassword",
  data() {
    return {
      idCardUrl: "",
      account: "",
      useRealRequest: {
        name: "",
        cardId: "",
        frontImg: "",
        oppositeImg: "",
      },
      getRealNames: {},
    };
  },
  components: {},
  mounted() {
    //this.getCode();
  },
  onLoad: function (options) {
    this.account = store.state.app.userInfo.phone;
    let that = this;
    // 获取系统信息
    uni.getSystemInfo({
      success(res) {
        that.platform = res.platform;
      },
    });
    const { code, state, scope, back_url, appleShow } = options;
    that.options = options;
    if (options.authKey) that.authKey = options.authKey;
    if (options.appleShow) that.appleShow = options.appleShow;

    this.getRealNameConfirm();
  },
  methods: {
	  async getUserInfomation() {
	  	let that = this
	  	await getUserInfo().then(res => { 
	  		that.$store.commit("UPDATE_USERINFO", res.data);
	  	})
	  },
    getRealNameConfirm() {
      getRealName().then((res) => {
        console.log("fanhuizhi", res);
        if (res.code == 200 && res.data !== null) {
          this.getRealNames = res.data;
          if (res.data.name != null || "") {
            this.useRealRequest.name = res.data.name;
          }
          if (res.data.cardId) {
            this.useRealRequest.cardId = res.data.cardId;
          }
        }
      });
    },
    upLoadPic(type) {
      let that = this;
      that.$util.uploadImageOne(
        {
          url: "user/upload/image",
          name: "multipart",
          model: "user",
          pid: "0",
          typeImg: type,
        },
        function (res) {
          const { url } = res.data;
          if (type == 1) {
            that.useRealRequest.frontImg = url;
			if(res.data.extData){
				that.useRealRequest.name = res.data.extData.name
				that.useRealRequest.cardId =  res.data.extData.number
			} 
          }
          if (type == 2) {
            that.useRealRequest.oppositeImg = url;
          }
          console.log("res========", url);
        }
      ),
        function (err) {
          that.$util.Tips({
            title: err,
          });
        };
    },
    async submit() {
      let that = this;
      if (Object.keys(that.getRealNames).length > 0)
        return that.$util.Tips({
          title: "已完成使命认证",
        });
      if (!that.useRealRequest.name)
        return that.$util.Tips({
          title: "请输入姓名",
        });
      if (!that.useRealRequest.cardId)
        return that.$util.Tips({
          title: "请输入身份证号",
        });
      const regex =
        /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/;
      if (!regex.test(that.useRealRequest.cardId))
        return that.$util.Tips({
          title: "请输入正确的身份证号码",
        });
      // if (!that.useRealRequest.frontImg)
      //   return that.$util.Tips({
      //     title: "请上传身份证正面图片",
      //   });
      // if (!that.useRealRequest.oppositeImg)
      //   return that.$util.Tips({
      //     title: "请上传身份证反面图片",
      //   });
      // that.useRealRequest.frontImg =
      //   that.useRealRequest.frontImg.split(".com/")[1];

      // that.useRealRequest.oppositeImg =
      //   that.useRealRequest.oppositeImg.split(".com/")[1];
      realNameConfirm(that.useRealRequest)
        .then((res) => {
          uni.showToast({
            title: res.message,
            icon: "success",
          });
		  that.getUserInfomation()
          setTimeout(() => {
            uni.navigateBack();
          }, 2000);
        })
        .catch((res) => {
          that.$util.Tips({
            title: res,
          });
        });
    },
  },
};
</script>

<style>
page {
  height: 100%;
}
</style>
<style lang="scss" scoped>
@import url("../../../static/css/gstyle.scss");

.appBox {
  background-color: #f5f6f7;
  height: 100%;
  padding: 33rpx 33rpx 16rpx 33rpx;

  .btn {
    margin-top: 400rpx;
  }

  .list-input {
    .item {
      border-bottom: 1px solid #f0f0f0;
      background: #fff;

      .row-middle {
        position: relative;
        padding: 0 25rpx;
        margin-top: 20rpx;

        .texts {
          flex: 1;
          font-size: 28rpx;
          height: 89rpx;
          line-height: 80rpx;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        input {
          flex: 1;
          font-size: 28rpx;
          height: 80rpx;
          line-height: 80rpx;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }

    .cards {
      display: flex;
      justify-content: space-between;
      margin-top: 83rpx;

      .card {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 25rpx;
        color: #333333;

        .img {
          width: 306rpx;
          height: 198rpx;
          margin-bottom: 38rpx;
        }
      }
    }
  }
}

page {
  background-color: #fff !important;
}
</style>
